module parameters
  use constants
  implicit none
  
  ! 媒质参数类型
  type :: media_parameters
    real(kind=8), allocatable :: z(:)      ! 界面深度
    real(kind=8), allocatable :: vp0(:)    ! P波速度
    real(kind=8), allocatable :: vs0(:)    ! S波速度
    real(kind=8), allocatable :: rho(:)    ! 密度
    real(kind=8), allocatable :: qs(:)     ! S波品质因子
    real(kind=8), allocatable :: qp(:)     ! P波品质因子
    real(kind=8), allocatable :: mu(:)     ! 剪切模量
    complex(kind=8), allocatable :: vs(:)  ! 复S波速度
    complex(kind=8), allocatable :: vp(:)  ! 复P波速度
  end type media_parameters
  
  ! 源参数类型
  type :: source_parameters
    real(kind=8) :: zs          ! 源深度
    real(kind=8) :: z0          ! 接收器深度
    real(kind=8) :: r0          ! 源-接收器距离
    real(kind=8) :: fai0        ! 方位角
    real(kind=8) :: fc          ! 中心频率
    real(kind=8) :: tou         ! 上升时间
    real(kind=8) :: M0          ! 标量矩
    real(kind=8) :: dip         ! 倾角
    real(kind=8) :: rake        ! 滑动角
    real(kind=8) :: str         ! 走向
    real(kind=8) :: mino, maxo  ! 频率范围
    real(kind=8) :: minv, maxv  ! 速度范围
    integer :: no, nv           ! 频率和速度点数
  end type source_parameters
  
  ! 计算参数类型
  type :: computation_parameters
    integer :: m, m1, mt, nt    ! 采样参数
    real(kind=8) :: L           ! 结构长度
    real(kind=8) :: Twin        ! 时间窗口
    real(kind=8) :: oi          ! 虚频
    real(kind=8) :: df, dt, dk  ! 频率、时间、波数间隔
    real(kind=8) :: vmin, vmax  ! 最小、最大速度
    real(kind=8) :: fmax        ! 最大频率
  end type computation_parameters
  
  ! 反射/透射系数类型
  type :: rt_coefficients
    complex(kind=8), allocatable :: rdu(:,:,:)   ! 下行反射系数
    complex(kind=8), allocatable :: rud(:,:,:)   ! 上行反射系数
    complex(kind=8), allocatable :: tdd(:,:,:)   ! 下行透射系数
    complex(kind=8), allocatable :: tuu(:,:,:)   ! 上行透射系数
    complex(kind=8), allocatable :: grdu(:,:,:)  ! 广义下行反射系数
    complex(kind=8), allocatable :: grud(:,:,:)  ! 广义上行反射系数
    complex(kind=8), allocatable :: gtu(:,:,:)   ! 广义上行透射系数
    complex(kind=8), allocatable :: gtd(:,:,:)   ! 广义下行透射系数
    complex(kind=8), allocatable :: rdu0(:)      ! SH波下行反射系数
    complex(kind=8), allocatable :: rud0(:)      ! SH波上行反射系数
    complex(kind=8), allocatable :: tdd0(:)      ! SH波下行透射系数
    complex(kind=8), allocatable :: tuu0(:)      ! SH波上行透射系数
    complex(kind=8), allocatable :: grdu0(:)     ! SH波广义下行反射系数
    complex(kind=8), allocatable :: grud0(:)     ! SH波广义上行反射系数
    complex(kind=8), allocatable :: gtu0(:)      ! SH波广义上行透射系数
    complex(kind=8), allocatable :: gtd0(:)      ! SH波广义下行透射系数
    complex(kind=8), allocatable :: ex(:,:)      ! 指数因子
  end type rt_coefficients
  
  ! 波动场参数类型
  type :: wavefield_parameters
    complex(kind=8), allocatable :: ur(:)   ! 径向位移
    complex(kind=8), allocatable :: uf(:)   ! 横向位移
    complex(kind=8), allocatable :: uz(:)   ! 垂向位移
  end type wavefield_parameters

  ! 全局变量定义
  type(media_parameters) :: media
  type(source_parameters) :: source
  type(computation_parameters) :: comp
  
  ! 其他参数
  integer :: nly          ! 层数
  integer :: lo, ls       ! 接收器和源所在的层
  character(len=10) :: SourceType, Case, Type_STF, Outname, OutFormat
  character(len=10) :: WinSwitch, WinType
  real(kind=8) :: f1, f2, f3, f4
  real(kind=8) :: coef, criterion
  
  ! 复波数
  complex(kind=8), allocatable :: cpn(:), csn(:)
  
  ! 单位矩阵
  complex(kind=8) :: unit(2,2)
  
  ! 指数因子
  complex(kind=8) :: exu(2), exd(2)
  
  ! 反射/透射系数
  type(rt_coefficients) :: rt_coef
  
  ! 源参数
  complex(kind=8) :: cigma0(2), cigma1(2), cigma2(2)
  complex(kind=8) :: cigma1_sh, cigma2_sh
  complex(kind=8) :: su0_1, su0_2, sd0_1, sd0_2
  complex(kind=8) :: su_0(2), su_1(2), su_2(2)
  complex(kind=8) :: sd_0(2), sd_1(2), sd_2(2)
  
  ! 波动场参数
  complex(kind=8) :: yu0, yd0
  complex(kind=8) :: yu(2,2), yd(2,2)
  
  
contains

  ! 初始化单位矩阵
  subroutine init_unit_matrix()
    unit(1,1) = 1.0d0
    unit(1,2) = 0.0d0
    unit(2,1) = 0.0d0
    unit(2,2) = 1.0d0
  end subroutine init_unit_matrix
  
  ! 初始化复波数数组
  subroutine init_complex_wavenumbers()
    if (allocated(cpn)) deallocate(cpn)
    if (allocated(csn)) deallocate(csn)
    allocate(cpn(NM), csn(NM))
  end subroutine init_complex_wavenumbers
  
  ! 初始化媒质参数
  subroutine init_media_parameters()
    if (allocated(media%z)) deallocate(media%z)
    if (allocated(media%vp0)) deallocate(media%vp0)
    if (allocated(media%vs0)) deallocate(media%vs0)
    if (allocated(media%rho)) deallocate(media%rho)
    if (allocated(media%qs)) deallocate(media%qs)
    if (allocated(media%qp)) deallocate(media%qp)
    if (allocated(media%mu)) deallocate(media%mu)
    if (allocated(media%vs)) deallocate(media%vs)
    if (allocated(media%vp)) deallocate(media%vp)
    
    allocate(media%z(0:NM))
    allocate(media%vp0(NM))
    allocate(media%vs0(NM))
    allocate(media%rho(NM))
    allocate(media%qs(NM))
    allocate(media%qp(NM))
    allocate(media%mu(NM))
    allocate(media%vs(NM))
    allocate(media%vp(NM))
  end subroutine init_media_parameters
  
  ! 初始化计算参数
  subroutine init_computation_parameters()
    comp%m = 0
    comp%m1 = 0
    comp%mt = 0
    comp%nt = 0
    comp%L = 0.0d0
    comp%Twin = 0.0d0
    comp%oi = 0.0d0
    comp%df = 0.0d0
    comp%dt = 0.0d0
    comp%dk = 0.0d0
    comp%vmin = 0.0d0
    comp%vmax = 0.0d0
    comp%fmax = 0.0d0
  end subroutine init_computation_parameters
  
  ! 初始化反射/透射系数
  subroutine init_rt_coefficients()
    if (allocated(rt_coef%rdu)) deallocate(rt_coef%rdu)
    if (allocated(rt_coef%rud)) deallocate(rt_coef%rud)
    if (allocated(rt_coef%tdd)) deallocate(rt_coef%tdd)
    if (allocated(rt_coef%tuu)) deallocate(rt_coef%tuu)
    if (allocated(rt_coef%grdu)) deallocate(rt_coef%grdu)
    if (allocated(rt_coef%grud)) deallocate(rt_coef%grud)
    if (allocated(rt_coef%gtu)) deallocate(rt_coef%gtu)
    if (allocated(rt_coef%gtd)) deallocate(rt_coef%gtd)
    if (allocated(rt_coef%rdu0)) deallocate(rt_coef%rdu0)
    if (allocated(rt_coef%rud0)) deallocate(rt_coef%rud0)
    if (allocated(rt_coef%tdd0)) deallocate(rt_coef%tdd0)
    if (allocated(rt_coef%tuu0)) deallocate(rt_coef%tuu0)
    if (allocated(rt_coef%grdu0)) deallocate(rt_coef%grdu0)
    if (allocated(rt_coef%grud0)) deallocate(rt_coef%grud0)
    if (allocated(rt_coef%gtu0)) deallocate(rt_coef%gtu0)
    if (allocated(rt_coef%gtd0)) deallocate(rt_coef%gtd0)
    if (allocated(rt_coef%ex)) deallocate(rt_coef%ex)
    
    allocate(rt_coef%rdu(NM,2,2))
    allocate(rt_coef%rud(0:NM,2,2))
    allocate(rt_coef%tdd(NM,2,2))
    allocate(rt_coef%tuu(NM,2,2))
    allocate(rt_coef%grdu(NM,2,2))
    allocate(rt_coef%grud(0:NM,2,2))
    allocate(rt_coef%gtu(NM,2,2))
    allocate(rt_coef%gtd(NM,2,2))
    allocate(rt_coef%rdu0(NM))
    allocate(rt_coef%rud0(0:NM))
    allocate(rt_coef%tdd0(NM))
    allocate(rt_coef%tuu0(NM))
    allocate(rt_coef%grdu0(NM))
    allocate(rt_coef%grud0(0:NM))
    allocate(rt_coef%gtu0(NM))
    allocate(rt_coef%gtd0(NM))
    allocate(rt_coef%ex(NM,2))
    
    ! 初始化为0
    rt_coef%rdu = 0.0d0
    rt_coef%rud = 0.0d0
    rt_coef%tdd = 0.0d0
    rt_coef%tuu = 0.0d0
    rt_coef%grdu = 0.0d0
    rt_coef%grud = 0.0d0
    rt_coef%gtu = 0.0d0
    rt_coef%gtd = 0.0d0
    rt_coef%rdu0 = 0.0d0
    rt_coef%rud0 = 0.0d0
    rt_coef%tdd0 = 0.0d0
    rt_coef%tuu0 = 0.0d0
    rt_coef%grdu0 = 0.0d0
    rt_coef%grud0 = 0.0d0
    rt_coef%gtu0 = 0.0d0
    rt_coef%gtd0 = 0.0d0
    rt_coef%ex = 0.0d0
  end subroutine init_rt_coefficients
  
  ! 初始化波动场参数
  subroutine init_wavefield_parameters()
    if (allocated(media%mu)) then
      if (.not. allocated(media%mu)) allocate(media%mu(NM))
      media%mu = 0.0d0
    end if
  end subroutine init_wavefield_parameters
  
  ! 初始化所有参数
  subroutine init_all_parameters()
    call init_unit_matrix()
    call init_complex_wavenumbers()
    call init_media_parameters()
    call init_computation_parameters()
    call init_rt_coefficients()
    call init_wavefield_parameters()
    
    ! 初始化其他参数
    nly = 0
    lo = 0
    ls = 0
    SourceType = ''
    Case = ''
    Type_STF = ''
    Outname = ''
    OutFormat = ''
    WinSwitch = ''
    WinType = ''
    f1 = 0.0d0
    f2 = 0.0d0
    f3 = 0.0d0
    f4 = 0.0d0
    coef = 0.0d0
    criterion = 0.0d0
    
    ! 初始化复数变量
    exu = 0.0d0
    exd = 0.0d0
    cigma0 = 0.0d0
    cigma1 = 0.0d0
    cigma2 = 0.0d0
    cigma1_sh = 0.0d0
    cigma2_sh = 0.0d0
    su0_1 = 0.0d0
    su0_2 = 0.0d0
    sd0_1 = 0.0d0
    sd0_2 = 0.0d0
    su_0 = 0.0d0
    su_1 = 0.0d0
    su_2 = 0.0d0
    sd_0 = 0.0d0
    sd_1 = 0.0d0
    sd_2 = 0.0d0
    yu0 = 0.0d0
    yd0 = 0.0d0
    yu = 0.0d0
    yd = 0.0d0
  end subroutine init_all_parameters
  
end module parameters